﻿using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using sqlmanagewpf.Bussiness;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;

namespace sqlmanagewpf
{
    internal class UserControl1ViewModel : BindableBase
    {
        private string _yourText;

        public string YourText
        {
            get { return _yourText; }
            set
            {
                if (_yourText != value)
                {
                    _yourText = value;
                    RaisePropertyChanged(nameof(YourText));
                }
            }
        }

        public ICommand AddCommand
        {
            get
            {
                return new DelegateCommand(() => {

                    StringBuilder strTrans = new StringBuilder();
                    YourText = "";
                    strTrans.Append("insert into grade(学号，姓名，数学) values(123,\"周润发\"，70)\n");
                    this.YourText = strTrans.ToString();
                    return;

                });
            }
        }

        public ICommand DeleteCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {

                    StringBuilder strTrans = new StringBuilder();
                    YourText = "";
                    strTrans.Append("delete from grade where 学号=123\n");
                    this.YourText = strTrans.ToString();
                    return;

                });
            }
        }

        public ICommand UpdateCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {

                    StringBuilder strTrans = new StringBuilder();
                    YourText = "";
                    strTrans.Append("update grade set 数学=80 where 学号=123\n");
                    this.YourText = strTrans.ToString();
                    return;

                });
            }
        }

        public ICommand QueryCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {

                    StringBuilder strTrans = new StringBuilder();
                    YourText = "";
                    strTrans.Append("select * from grade where 学号=123\n");
                    this.YourText = strTrans.ToString();
                    return;

                });
            }
        }

        public ICommand NavigateCommand
        {
            get
            {
                return new DelegateCommand<TreeNodeConfig>(Navigate);
            }
        }

        TreeNodeConfig myselectedItem;

        private void Navigate(TreeNodeConfig selectedItem)
        {
            myselectedItem = selectedItem;

            var selectedNode = selectedItem as TreeNodeConfig;
            if (selectedNode != null&&selectedNode.NodeType==NodeType.table)
            {
                string strSql = "select top 10 * from " + database + "..[" + selectedItem.Name + "]";
                DatabaseInfos = SqlServerHelper.getDataTable(strSql);

            }
        }
        private DataTable _DatabaseInfos;
        public DataTable DatabaseInfos
        {
            get { return _DatabaseInfos; }
            set
            {
                if (_DatabaseInfos != value)
                {
                    _DatabaseInfos = value;
                    RaisePropertyChanged(nameof(DatabaseInfos));
                }
            }
        }
        //public ObservableCollection<TreeNodeConfig> TreeNodes { get; set; }

        private ObservableCollection<TreeNodeConfig> _treeNodes;

        public ObservableCollection<TreeNodeConfig> TreeNodes
        {
            get { return _treeNodes; }
            set
            {
                if (_treeNodes != value)
                {
                    _treeNodes = value;
                    RaisePropertyChanged(nameof(TreeNodes));
                }
            }
        }

        private readonly IEventAggregator _eventAggregator;

        public UserControl1ViewModel(IEventAggregator eventAggregator)
        {
            maketree();
            //macktree();
            //connectEvent 注册这个事件
            _eventAggregator = eventAggregator;

            _eventAggregator.GetEvent<connectEvent>().Subscribe(OnConnectEvent);
        }
        string database = "";
        private void OnConnectEvent(DatabaseConfig config)
        {
            database = config.Database;
            TreeNodes = new ObservableCollection<TreeNodeConfig>();
            TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
            treeNodeConfig.Name = config.Server;
            treeNodeConfig.IsExpanded = true;
            treeNodeConfig.Children = new ObservableCollection<TreeNodeConfig>();
            TreeNodes.Add(treeNodeConfig);

            TreeNodeConfig treeNodedb = new TreeNodeConfig();
            treeNodedb.Name = config.Database;
            treeNodedb.IsExpanded = true;
            treeNodedb.Children = new ObservableCollection<TreeNodeConfig>();
            treeNodeConfig.Children.Add(treeNodedb);

            string strSql = "use " + config.Database + ";" + "select * from sysobjects where type='U' and name<>'sysdiagrams'";
            System.Data.SqlClient.SqlDataReader thisReader = SqlServerHelper.GetReader(strSql);
            while (thisReader.Read())
            {
                TreeNodeConfig tn = new TreeNodeConfig();
                tn.Name = thisReader[0].ToString();
                tn.NodeType = NodeType.table;
                tn.Children = new ObservableCollection<TreeNodeConfig>();
                //tn.IsExpanded = true;
                treeNodedb.Children.Add(tn);

                var strSql1 = "use " + config.Database + ";select   a.[name] as lieming,b.[name] as leixing    from   syscolumns a,systypes b  where   a.id=object_id('"
      + config.Database + ".." + tn.Name + "') and a.xtype=b.xtype and    b.[name]<>'sysname'";
                DataTable dt = SqlServerHelper.getDataTable(strSql1);
                foreach (DataRow dr in dt.Rows)
                {
                    string field = dr["lieming"].ToString();
                    string type = dr["leixing"].ToString();
                    //string nullvalue = dr["null"].ToString();
                    //string key = dr["key"].ToString();
                    //string extra = dr["extra"].ToString();
                    //string comment = dr["comment"].ToString();

                    string result = "";
                    result += field;
                    result += " [";
                    result += type + "]";

                    TreeNodeConfig tn1 = new TreeNodeConfig();
                    tn1.Name = result;

                    tn.Children.Add(tn1);
                }
            }


        }

        private void maketree()
        {
        }

        public void macktree()
        {
            TreeNodes = new ObservableCollection<TreeNodeConfig>
            {
            new TreeNodeConfig
            {
                Name = "stt-2012r2",
                IsExpanded=true,
                Children = new ObservableCollection<TreeNodeConfig>
                {
                    new TreeNodeConfig
                    {
                        Name = "TOPS",
                IsExpanded=true,

                        Children = new ObservableCollection<TreeNodeConfig>
                        {
                            new TreeNodeConfig { Name = "All_Supported_Mfr", IsExpanded=true},
                            new TreeNodeConfig
                            {
                                IsExpanded=false,
                                Name = "All_Supported_Model",
                                Children = new ObservableCollection<TreeNodeConfig>
                                {
                                    new TreeNodeConfig { Name = "CreateDate [datetime]" ,IsExpanded=false},
                                    new TreeNodeConfig { Name = "Creater [varchar]" },
                                    new TreeNodeConfig { Name = "DbVersion [varchar]" },
                                    new TreeNodeConfig { Name = "IsActive [bit]" },
                                    new TreeNodeConfig { Name = "Mfr_GUID [uniqueidentifier]" },
                                    new TreeNodeConfig { Name = "Model_Description [varchar]" },
                                    new TreeNodeConfig { Name = "Model_FullName [varchar]" },
                                    new TreeNodeConfig { Name = "Model_GUID [uniqueidentifier]" },
                                    new TreeNodeConfig { Name = "Model_ID [bigint]" },
                                    new TreeNodeConfig { Name = "Model_Name [varchar]" },
                                }
                            }
                        }
                    }
                }
            }
        };
        }
    }
}